package com.szlanyou.thirdsdk.retrofit.convert;

import com.szlanyou.thirdsdk.retrofit.callback.IHttpResultHandler;
import com.szlanyou.thirdsdk.retrofit.util.ReflectManger;

import java.io.IOException;

import io.reactivex.Observable;
import okhttp3.ResponseBody;

/**
 * @author 陈章
 * create at 2021/7/8 13:30
 * desc:处理ResponseBody的抽象类
 * 让使用者实现具体的转换逻辑
 */
public abstract class AbsResponseBodyHttpResultHandler<T>  extends IHttpResultHandler<ResponseBody> {
    private IHttpResultHandler<T> httpResultHandler;

    public AbsResponseBodyHttpResultHandler(IHttpResultHandler<T> httpResultHandler) {
        this.httpResultHandler = httpResultHandler;
    }

    public AbsResponseBodyHttpResultHandler() {
    }

    /**
     * see{@link com.szlanyou.thirdsdk.retrofit.client.HttpClient#createResponseBodyHttpResultHandler}，将httpResultHandler
     * 关联给组件设置的AbsResponseBodyHttpResultHandler.
     * @param httpResultHandler
     */
    public void attach(IHttpResultHandler<T> httpResultHandler){
        this.httpResultHandler = httpResultHandler;
    }

    @Override
    public void onStart(String requestPageFlag,String requestCode) {
        if (httpResultHandler != null) {
            httpResultHandler.onStart(requestPageFlag,requestCode);
        }
    }

    @Override
    public void onSuccess(String requestPageFlag,String requestCode, ResponseBody successBean) {
        if (httpResultHandler != null) {
            try {
                Class httpResultHandlerClass = ReflectManger.getGeneric(httpResultHandler);
                onReceiveJson(requestPageFlag,requestCode,successBean.string(),httpResultHandler,httpResultHandlerClass);
            } catch (IOException e) {
                e.printStackTrace();
                onError(requestPageFlag,requestCode,e);
            }
        }
    }

    /**
     * 获取到json数据
     * @param requestCode               请求码
     * @param json                      json数据
     * @param httpResultHandler         原始的结果处理句柄
     * @param httpResultHandlerClass    结果处理句柄的泛型字节码  (以便于调用者判断给httpResultHandler返回哪段值)
     */
    public abstract void onReceiveJson(String requestPageFlag,String requestCode,  String json, IHttpResultHandler<T> httpResultHandler, Class httpResultHandlerClass);


    @Override
    public void onError(String requestPageFlag,String requestCode, Throwable e) {
        if (httpResultHandler != null) {
            httpResultHandler.onError(requestPageFlag,requestCode, e);
        }
    }

    @Override
    public void onFailure(String requestPageFlag,String requestCode, String responseCode, String msg) {
        if (httpResultHandler != null) {
            httpResultHandler.onFailure(requestPageFlag,requestCode, responseCode, msg);
        }
    }

    @Override
    public void onCancel(String requestPageFlag,String requestCode) {
        if (httpResultHandler != null) {
            httpResultHandler.onCancel(requestPageFlag,requestCode);
        }
    }

    @Override
    public void onComplete(boolean causeByError) {
        if (httpResultHandler != null) {
            httpResultHandler.onComplete(causeByError);
        }
    }

    @Override
    public IHttpResultHandler getOriginalHttpResultHandler() {
        return httpResultHandler;
    }
}